/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lambo.maven.core.utils.superHttp.utils;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

/**
 * http 配置工具类
 * @author Jianbo
 */
public class ConfigUtil {

    public static void init(HttpResponse response) {
        setCookieStore(response);
    }
    
    public static void setCookieStore(HttpResponse httpResponse) {
        if(httpResponse == null) {
            throw new RuntimeException("httpresponse is null");
        }
//        System.out.println("----setCookieStore");
        BasicCookieStore cookieStore = new BasicCookieStore();
        // JSESSIONID
        String JSESSIONID;
        try {
            String setCookie = httpResponse.getFirstHeader("Set-Cookie")
                .getValue();
            JSESSIONID = setCookie.substring("JSESSIONID=".length(),
                setCookie.indexOf(";"));
        }catch (Exception e) {
            JSESSIONID = "";
        }
//        System.out.println("JSESSIONID:" + JSESSIONID);
        // 新建一个Cookie
        BasicClientCookie cookie = new BasicClientCookie("JSESSIONID",
                JSESSIONID);
        cookie.setVersion(0);
        cookie.setDomain("127.0.0.1");
        cookie.setPath("/CwlProClient");
        // cookie.setAttribute(ClientCookie.VERSION_ATTR, "0");
        // cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "127.0.0.1");
        // cookie.setAttribute(ClientCookie.PORT_ATTR, "8080");
        // cookie.setAttribute(ClientCookie.PATH_ATTR, "/CwlProWeb");
        cookieStore.addCookie(cookie);
    }
    
}
